package lambdasinaction.chap2;

import java.util.Arrays;
import java.util.List;

/**
 * 美化打印苹果
 *
 * @author Fish
 * @since 0.0.1 2018/7/15 17:57
 */
public class PrettyPrintApples {

  public static void main(String[] args) {
    List<Apple> inventory = Arrays.asList(new Apple("red", 145),
        new Apple("green", 130),
        new Apple("red", 155),
        new Apple("green", 30));

//    prettyPrintApple(inventory, new AppleFancyFormatter());


    prettyPrintApple(inventory, new AppleSimpleFormatter());
  }

  public static void prettyPrintApple(List<Apple> inventory, AppleFormatter formatter) {
    for (Apple apple : inventory) {
      String output = formatter.accept(apple);
      System.out.println(output);
    }
  }

  public static class AppleFancyFormatter implements AppleFormatter {

    @Override
    public String accept(Apple apple) {
      String characteristic = apple.getWeight() > 150 ? "heavy" : "light";
      return "A " + characteristic + " " + apple.getColor() + " apple";
    }
  }

  public static class AppleSimpleFormatter implements AppleFormatter {

    @Override
    public String accept(Apple apple) {
      return "An apple of " + apple.getWeight() + "g";
    }
  }

  interface AppleFormatter {
    String accept(Apple apple);
  }

  public static class Apple {
    private String color;
    private Integer weight;

    public Apple(String color, Integer weight) {
      this.color = color;
      this.weight = weight;
    }

    public String getColor() {
      return color;
    }

    public void setColor(String color) {
      this.color = color;
    }

    public Integer getWeight() {
      return weight;
    }

    public void setWeight(Integer weight) {
      this.weight = weight;
    }

    @Override
    public String toString() {
      return "Apple{" +
          "color='" + color + '\'' +
          ", weight=" + weight +
          '}';
    }
  }

}
